// index/details.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    productInfo: {},
    showModalStatus: false,
    goods_img: [],
    animationData: '',
    indicatorDots: true,
    autoplay: true,
    interval: 5000,
    duration: 1000,
    buyQty: 1,
    action: 'addcar'
  },

  //对轮播图进行预览 
  // previewImage: function(e) {
  //   var current = e.target.dataset.src;
  //   var href = this.data.imghref;
  //   var goodsimg = this.data.goods_img;
  //   var imglist = [];
  //   for (var i = 0; i < goodsimg.length; i++) {
  //     imglist[i] = href + goodsimg[i].img
  //   }
  //   wx.previewImage({
  //     current: current, // 当前显示图片的http链接  
  //     urls: imglist // 需要预览的图片http链接列表  
  //   })
  // },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var info = JSON.parse(options.Mesgs);
    let imageUrl = app.globalData.serverUrl + '/download?imageFileName=';
    info.image=imageUrl+info.image+".jpg";
    var action = info.action;
    if (!action) {
      action = 'addcar';
    }
    let serverUrl = app.globalData.serverUrl + '/getImagesByProductId?productId=' + info.id;
    var self = this;
    wx.request({
      url: serverUrl,
      method: 'GET',
      success: function(result) {
        var imageList = [];
        for (let image of result.data.data) {
          imageList.push(image);
        }
        self.setData({
          productInfo: info,
          goods_img: imageList,
          action: action
        })
      }
    })
    if (info.action) {
      this.buy();
    }

  },
  addShopCar: function() {
    //购物车代码
    //https://www.jianshu.com/p/b01e76034e07
    let isFind = false;
    let p1 = this.data.productInfo;
    var arr = wx.getStorageSync('car') || [];
    var qty = parseInt(this.data.buyQty);
    for (let p of arr) {
      if (p.name == p1.name) {
        qty = parseInt(p.num) + parseInt(this.data.buyQty);
        p.num=qty;
        isFind = true;
        break;
      }
    }
    if (!isFind) {
      arr.push({
        id: p1.id,
        name: p1.name,
        num: qty,
        price: p1.price
      })
    }
    wx.setStorageSync('car', arr);
    console.log("加入购物车" + this.data.buyQty);

  },
  showModal: function() {
    // 显示遮罩层
    var animation = wx.createAnimation({
      duration: 200,
      timingFunction: "ease-in-out",
      delay: 0
    })
    this.animation = animation
    animation.translateY(500).step()
    this.setData({
      animationData: animation.export(),
      showModalStatus: true
    })
    setTimeout(function() {
      animation.translateY(0).step()
      this.setData({
        animationData: animation.export()
      })
    }.bind(this), 200)
  },
  hideModal: function() {
    if (this.data.action == 'addcar') {
      this.addShopCar();
      //关闭窗口
      wx.showToast({
        title: '加入购物车成功！',
        icon: 'success',
        duration: 2000
      });
    } else { //立即购买
      var orderInfo = [];
      orderInfo.push(this.data.productInfo);
      var mesg = JSON.stringify(orderInfo);
      wx.navigateTo({
        //在接收页面的url后面加上“？自定义名称=字符串”就可以通过url传值
        url: '../orderConfirm/orderConfirm?orderInfo=' + mesg
      })
    }

    this.setData({
      showModalStatus: false,
    })
  },
  qtyChange: function(e) {
    var self = this;
    self.setData({
      buyQty: e.detail.value
    })
  },
  addQty: function(e) {
    var qty = parseInt(this.data.buyQty) + 1;
    this.setData({
      buyQty: qty
    })
  },
  subQty: function(e) {
    var qty = parseInt(this.data.buyQty) - 1;
    this.setData({
      buyQty: qty
    })
  },
  outbtn: function(e) {
    this.setData({
      showModalStatus: false,
    })
  },
  addcar: function(e) {
    this.setData({
      action: 'addcar'
    });
    this.showModal();
  },
  buy: function(e) {
    this.setData({
      action: 'buy'
    });
    this.showModal();
  },
  tocar:function(e){
    wx.switchTab({
      url: '../cart/cart',
    })
  }
})